﻿using System.Collections.Generic;
using System.Windows.Forms;
using ReagentStore.BLL;
using ReagentStore.ERM;
using ReagentStore.UTL;

namespace ReagentStore
{
	public partial class ReagentDialog : Form
	{
		private readonly PassParamsHandler<Reagent> _feedbackHandler;
		private readonly DialogParam<Reagent> _param;
		private FormValidator _formValidator;

		public ReagentDialog(DialogParam<Reagent> param, PassParamsHandler<Reagent> handler)
		{
			InitializeComponent();

			_feedbackHandler = handler;
			_param = param;

			InitializeCombo();
			InitializeControlsByParam();
			InitializeValidator();
		}

		private void InitializeCombo()
		{
			IList<Catalog> catalogs = new CatalogBLL().DALAccessor.GetEntities();
			comboCatalog.DataSource = catalogs;
			comboCatalog.DisplayMember = "Name";
			comboCatalog.ValueMember = "CatalogID";

			comboCatalog.SelectedIndex = -1;
		}

		private void InitializeControlsByParam()
		{
			Reagent reagent = _param.Parameter;
			switch (_param.ActionType)
			{
				case ActionTypes.Insert:
					Text = "添加试剂";
					comboCatalog.SelectedValue = reagent.CatalogID > 0 ? reagent.CatalogID : -1;
					break;

				case ActionTypes.Update:
					Text = "更新试剂";
					comboCatalog.SelectedValue = reagent.CatalogID;
					textName.Text = reagent.Name;
					textUnit.Text = reagent.Unit;
					numStandingCount.Value = reagent.StandingCount;
					numAlertCount.Value = reagent.AlertCount;
					numAlertPeriod.Value = reagent.AlertPeriod;
					break;

				case ActionTypes.Delete:
					Text = "删除试剂";
					comboCatalog.SelectedValue = reagent.CatalogID;
					textName.Text = reagent.Name;
					textUnit.Text = reagent.Unit;
					numStandingCount.Value = reagent.StandingCount;
					numAlertCount.Value = reagent.AlertCount;
					numAlertPeriod.Value = reagent.AlertPeriod;

					groupBox.Enabled = false;
					break;
			}
		}

		private void InitializeValidator()
		{
			_formValidator = new FormValidator(errorProvider);

			_formValidator.Add(new RequiredValidator(textName, "请输入试剂名称"));
			_formValidator.Add(new RequiredValidator(comboCatalog, "请选择试剂分类"));
			_formValidator.Add(new RequiredValidator(textUnit, "请输入计量单位"));
			_formValidator.Add(new DecimalRangeValidator(numStandingCount, "常备量应大于预警量", 1, decimal.MaxValue));
			_formValidator.Add(new DecimalRangeValidator(numAlertCount, "常备量应大于预警量", 1, 1));
		}

		private void Feedback()
		{
			switch (_param.ActionType)
			{
				case ActionTypes.Insert:
				case ActionTypes.Update:
					_param.Parameter.CatalogID = (int) comboCatalog.SelectedValue;
					_param.Parameter.Name = textName.Text;
					_param.Parameter.Unit = textUnit.Text;
					_param.Parameter.StandingCount = (int) numStandingCount.Value;
					_param.Parameter.AlertCount = (int) numAlertCount.Value;
					_param.Parameter.AlertPeriod = (int) numAlertPeriod.Value;
					break;
				case ActionTypes.Delete:
					break;
			}

			PassParamsHandler<Reagent> handler = _feedbackHandler;
			if (handler != null)
				handler(_param);
		}

		private void ReagentDialog_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DialogResult != DialogResult.OK)
				return;


			if (_param.ActionType == ActionTypes.Insert)
			{
				decimal standingcount = numStandingCount.Value;
				decimal alertcount = numAlertCount.Value;

				((DecimalRangeValidator) _formValidator[numStandingCount]).Min = alertcount + 1;
				((DecimalRangeValidator) _formValidator[numAlertCount]).Max = standingcount - 1;

				if (!_formValidator.IsValid())
				{
					e.Cancel = true;
					return;
				}
			}

			if (_param.ActionType == ActionTypes.Delete)
			{
				const string pattern = "删除该试剂，将会导致该试剂所属的产品及出入库记录一并被删除。\r\n确认执行吗?";
				DialogResult confirm = MessageBox.Show(pattern, "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
				if (confirm == DialogResult.No)
					return;
			}

			Feedback();
		}
	}
}